package com.hzgj.soa.protocol.pb.data;

import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
import com.hzgj.bcl.soa.rpc.message.Payload;
import com.hzgj.soa.protocol.pb.encode.SimpleEncoder;
import lombok.Getter;

import java.util.List;

/**
 * Created by noname on 15/12/5.
 */
public class RequestMessage implements Payload {

    @Protobuf(order = 1, required = true)
    @Getter
    public String clientName;

    @Protobuf(order = 2, required = true)
    @Getter
    public String service;

    @Protobuf(order = 3, required = true)
    @Getter
    public String method;

    @Protobuf(order = 4)
    public List<SimpleValue> parameters;

    /**
     * 上下文 ID, 在整个请求链中保持不变
     */
    @Protobuf(order = 5)
    @Getter
    public String contextId;

    /**
     * 消息 ID, 每个请求唯一
     */
    @Protobuf(order = 6, required = true)
    public Long messageId;

    @Protobuf(order = 7)
    @Getter
    public String bizId;

    @Override
    public Object[] getArgs(Class<?>[] types) {
        if (parameters == null) {
            return new Object[]{};
        }
        Object[] args = new Object[parameters.size()];
        for (int i = 0; i < args.length; i++) {
            SimpleValue value = parameters.get(i);
            args[i] = SimpleEncoder.decode(value.dataType, value.data, types[i]);
        }
        return args;
    }
}
